﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.Common;

namespace Think.Lenovo.Com.DataAccess
{
    public class RolePowerRelation
    {
        /// <summary>
        /// 添加一个角色的一个权限
        /// </summary>
        /// <param name="model">角色和权限的关系实体</param>
        /// <returns>自增id</returns>
        public long AddRolePowerRelation(RolePowerRelationModel model)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append("tbl_role_power_r (roleId,powerId)")
                .Append("values(@roleId,@powerId)");


            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@roleId",MySqlDbType.Int32),
                    new MySqlParameter("@powerId",MySqlDbType.Int32)
                };

            ps[0].Value = model.RoleId;
            ps[1].Value = model.PowerId;

            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;

        }
        /// <summary>
        /// 添加一个角色的一个权限
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <param name="powerId">角色id</param>
        /// <returns>自增id</returns>
        public long AddRolePowerRelation(int roleId, int powerId)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append("tbl_role_power_r (roleId,powerId)")
                .Append("values(@roleId,@powerId)");


            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@roleId",MySqlDbType.Int32),
                    new MySqlParameter("@powerId",MySqlDbType.Int32)
                };

            ps[0].Value = roleId;
            ps[1].Value = powerId;

            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;

        }
        /// <summary>
        /// 给定一角色id 和若干个权限id,把该角色与权建立关系
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <param name="powerIds">权限id集合</param>
        /// <returns>是否成功 true :表示成功 false:表示失败</returns>
        public bool AddRolePowerRelation(int roleId, IEnumerable<int> powerIds)
        {
            if (roleId > 0)
            {
                IEnumerator<int> ie = powerIds.GetEnumerator();
                if (!ie.MoveNext())
                {
                    return false;
                }
                ie.Reset();
                while (ie.MoveNext())
                {
                    long id = AddRolePowerRelation(roleId, ie.Current);
                    if (id < 0)
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 根据一个角色id得到该角色的所有权限
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <returns>角色和权限的关系实体集合</returns>
        public LinkedList<RolePowerRelationModel> GetRolePowerRelationModels(int roleId)
        {
            LinkedList<RolePowerRelationModel> lst = new LinkedList<RolePowerRelationModel>();

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Select * From  tbl_role_power_r ")
                .Append("Where roleId=" + roleId);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            if (ds != null)
            {
                if (ds.Tables[0] != null)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        RolePowerRelationModel model = new RolePowerRelationModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.PowerId = Convert.ToInt32(row["powerId"]);
                        model.RoleId = Convert.ToInt32(row["roleId"]);
                        lst.AddLast(model);
                    }
                }
            }
            return lst;

        }

        /// <summary>
        /// 将指定的角色与权限断开关联,也就是相当于清除角色的权限
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <returns>受影响的行数</returns>
        public int DisconnectRelaction(int roleId)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Delete From ")
                .Append(" tbl_role_power_r ")
                .Append(" Where roleId = @roleId");


            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@roleId",MySqlDbType.Int32)
                };

            ps[0].Value = roleId;

            long affect = MysqlHelper.Delete(conn, sql.ToString(), ps);
            return (int)affect;

        }
    }
}
